home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / threads / ThreadsSharedMemBSDUtils.c < prev    next >
C/C++ Source or Header  |  1990-11-28  |  4KB  |  165 lines

  1. /* begincopyright
  2.    Copyright (c) 1988 Xerox Corporation. All rights reserved.
  3.    Use and copying of this software and preparation of derivative works based
  4.    upon this software are permitted. Any distribution of this software or
  5.    derivative works must comply with all applicable United States export
  6.    control laws. This software is made available AS IS, and Xerox Corporation
  7.    makes no warranty about the software, its performance or its conformity to
  8.    any specification. Any person obtaining a copy of this software is requested
  9.    to send their name and post office or electronic mail address to:
  10.    PCR Coordinator
  11.    Xerox PARC
  12.    3333 Coyote Hill Rd.
  13.    Palo Alto, CA 94304
  14.    endcopyright */
  15.  
  16. /*
  17.  * ThreadsSharedMemBSDUtils.c
  18.  *
  19.  * Demers, November 28, 1990 11:09:56 am PST
  20.  *
  21.  */
  22.  
  23.  
  24. #include "xr/BasicTypes.h"
  25. #include "xr/ThreadsSharedMemBSDUtils.h"
  26. #include "xr/Errno.h"
  27.  
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/file.h>
  31. #include <sys/stat.h>
  32.  
  33.  
  34.  
  35. extern void bcopy(/* char *from, char *to, int nbytes */);
  36.  
  37.  
  38. static char
  39. XR_sharedMemBSDSwapDir[FILE_NAME_BUF_SIZE] = DEFAULT_SWAP_DIR;
  40.  
  41. static int
  42. XR_sharedMemBSDSwapDirIndex = DI_NONE;
  43.  
  44.  
  45. int
  46. XR_SharedMemBSDSetSwapDir(dir, index)
  47.     char *dir;
  48.     int index;
  49. {
  50.     if( dir != NIL ) {
  51.         if( strlen(dir) >= (sizeof XR_sharedMemBSDSwapDir) )
  52.             return (-ENAMETOOLONG);
  53.         (void)strcpy(XR_sharedMemBSDSwapDir, dir);
  54.     }
  55.     XR_sharedMemBSDSwapDirIndex = index;
  56.     return 0;
  57. }
  58.  
  59.  
  60. int
  61. XR_SharedMemBSDGetSwapDir(dirBuf, dirBufLen, indexBuf)
  62.     char *dirBuf;
  63.     int dirBufLen;
  64.     int *indexBuf;
  65. {
  66.     if( dirBuf != NIL ) {
  67.         if( strlen(XR_sharedMemBSDSwapDir) >= dirBufLen )
  68.             return (-ENAMETOOLONG);
  69.         strcpy(dirBuf, XR_sharedMemBSDSwapDir);
  70.     }
  71.     if( indexBuf != NIL ) {
  72.         *indexBuf = XR_sharedMemBSDSwapDirIndex;
  73.     }
  74.     return 0;
  75. }
  76.  
  77.  
  78. static char *
  79. XR_SharedMemBSDAppendNum(buf, num)
  80.     char *buf;
  81.     unsigned num;
  82. {
  83.     char b[12];
  84.     char *pb = b + (sizeof b);
  85.  
  86.     *--pb = 0;
  87.     for(;;) {
  88.         *(--pb) = ('0' + (num % 10));
  89.         if( (num /= 10) == 0 ) break;
  90.     }
  91.     for(;;) {
  92.         if( (*buf = *(pb++)) == 0 ) return buf;
  93.         buf++;
  94.     }
  95. }
  96.  
  97. int /* -errno */
  98. XR_SharedMemBSDMkFileName(buf, bufLen, shortName, fileIndex)
  99.     char *buf;
  100.     int bufLen;
  101.     char *shortName;
  102.     int fileIndex;
  103. {
  104.     int dirNameLen, shortNameLen;
  105.     char *p = buf;
  106.  
  107.     /* not done with sprintf because can be called with very    */
  108.     /* limited stack space on panic ...                */
  109.  
  110.     dirNameLen = strlen(XR_sharedMemBSDSwapDir);
  111.     shortNameLen = ( (shortName != NIL) ? strlen(shortName) : 0 );
  112.     if( (dirNameLen + shortNameLen + FILE_NAME_SLOP) > bufLen )
  113.         return (-ENAMETOOLONG);
  114.  
  115.     (void)strcpy(p, XR_sharedMemBSDSwapDir);  p += dirNameLen;
  116.     if( XR_sharedMemBSDSwapDirIndex == DI_NONE ) return 0;
  117.     *p++ = '/';
  118.     p = XR_SharedMemBSDAppendNum(p, XR_sharedMemBSDSwapDirIndex);
  119.     if( shortNameLen == 0 ) return 0;
  120.     *p++ = '/';
  121.     (void)strcpy(p, shortName);  p += shortNameLen;
  122.     if( fileIndex != FI_NONE ) p = XR_SharedMemBSDAppendNum(p, fileIndex);
  123.     return 0;
  124. }
  125.  
  126.  
  127. int
  128. XR_SharedMemBSDGetBackingFile(shortName, index, setLen, fdp)
  129.     char *shortName;
  130.     int index;
  131.     int setLen; /* < 0 ==> don't change */
  132.     int *fdp;
  133. {
  134.     int fd, ans;
  135.     bool stickyFailed = FALSE;
  136.     struct stat statBuf;
  137.     char fnBuf[FILE_NAME_BUF_SIZE];
  138.  
  139.     ans = XR_SharedMemBSDMkFileName(fnBuf, (sizeof fnBuf), shortName, index);
  140.     if( ans < 0 ) return (ans);
  141.     fd = open(fnBuf, O_RDWR, 0666);
  142.     if( fd < 0 ) {
  143.     fd = open(fnBuf, O_RDWR|O_CREAT, 0666);
  144.     if( fd < 0 ) return (-EIO);
  145.     }
  146.     ans = fstat(fd, &statBuf);
  147.     if( ans < 0 ) return (-EIO);
  148.     if( (statBuf.st_mode & S_ISVTX) == 0 ) {
  149.         if( geteuid() == 0 ) {
  150.             ans = fchmod( fd, (statBuf.st_mode | S_ISVTX) );
  151.             stickyFailed = (ans != 0);
  152.         } else {
  153.             stickyFailed = TRUE;
  154.         }
  155.     }
  156.     if( (setLen >= 0) && (statBuf.st_size != setLen) ) {
  157.     ans = ftruncate(fd, setLen);
  158.     if( ans < 0 ) { (void)close(fd); return (-EIO); }
  159.     }
  160.     *fdp = fd;
  161.     return (stickyFailed ? 1 : 0);
  162. }
  163.  
  164.  
  165.